﻿#region Using statements
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
#endregion



namespace SmartFrequency.Utils
{
    class GRoundedRectangles
    {
        public delegate void functionPointer();

        #region Private variables
        struct ButtonFontSettings_s
        {
            private string _name;
            public string name
            {
                set { _name = value; }
                get { return _name; }
            }

            private int _size;
            public int size
            {
                set { _size = value; }
                get { return _size; }
            }

            private FontStyle _style;
            public FontStyle style
            {
                set { _style = value; }
                get { return _style; }
            }
        };

        struct ColorSettings_s
        {
            private Color _lineColor;
            public Color lineColor
            {
                set { _lineColor = value; }
                get { return _lineColor; }
            }

            private Color _foreColor;
            public Color foreColor
            {
                set { _foreColor = value; }
                get { return _foreColor; }
            }

            private Color _blinkColor;
            public Color blinkColor
            {
                set { _blinkColor = value; }
                get { return _blinkColor; }
            }

            private Color _fontColor;
            public Color fontColor
            {
                set { _fontColor = value; }
                get { return _fontColor; }
            }
        };

        struct ButtonSettings_s
        {
            private ColorSettings_s _color;
            public ColorSettings_s color
            {
                set { _color = value; }
                get { return _color; }
            }

            private int _left;
            public int left
            {
                set { _left = value; }
                get { return _left; }
            }

            private int _top;
            public int top
            {
                set { _top = value; }
                get { return _top; }
            }

            private int _width;
            public int width
            {
                set { _width = value; }
                get { return _width; }
            }

            private int _height;
            public int height
            {
                set { _height = value; }
                get { return _height; }
            }

            private int _lineLength;
            public int lineLength
            {
                set { _lineLength = value; }
                get { return _lineLength; }
            }

            private string _text;
            public string text
            {
                set { _text = value; }
                get { return _text; }
            }

            private ButtonFontSettings_s _font;
            public ButtonFontSettings_s font
            {
                set { _font = value; }
                get { return _font; }
            }

            private functionPointer _function;
            public functionPointer function
            {
                set { _function = value; }
                get { return _function; }
            }

        };

        List<ButtonSettings_s> buttons = new List<ButtonSettings_s>();

        private Graphics gfx;
        #endregion

        #region Public variables
        public Color lineColor = Color.Black, foreColor = Color.Gray, blinkColor = Color.White, fontColor = Color.Black;
        public string fontName = "Tahoma";
        public int fontSize = 9;
        public FontStyle fontStyle = FontStyle.Bold;
        public int lineLength = 2;
        #endregion

        private void createGraphics(Graphics gfx1, int x, int y, int width, int height, ColorSettings_s color, string text, ButtonFontSettings_s font)
        {
            int radius = height / 2;
            Pen p = new Pen(lineColor, (float)lineLength);
            Font textFont = new Font(font.name, font.size, font.style);
            SizeF textSize;
            SolidBrush solidColorFore = new SolidBrush(color.foreColor);
            SolidBrush solidColorLine = new SolidBrush(color.lineColor);
            SolidBrush solidColorFont = new SolidBrush(color.fontColor);

            // First draw outer space
            /*
            gfx1.DrawEllipse(p, x, y, height, height);
            gfx1.DrawEllipse(p, x + width - radius, y, height, height);
            gfx1.DrawLine(p, x + radius, y, x + width, y);
            gfx1.DrawLine(p, x + radius, y + height, x + width, y + height);
             */

            gfx1.FillEllipse(solidColorLine, x, y, height, height);
            gfx1.FillEllipse(solidColorLine, x + width - radius, y, height, height);
            gfx1.FillRectangle(solidColorLine, x + radius, y, width - radius, height + (lineLength/2));

            // Second draw inner space
            gfx1.FillEllipse(solidColorFore, x + lineLength, y + lineLength, height - (2 * lineLength), height - (2 * lineLength));
            gfx1.FillEllipse(solidColorFore, x + width - radius + lineLength, y + lineLength, height - (2 * lineLength), height - (2 * lineLength));
            gfx1.FillRectangle(solidColorFore, x + radius + lineLength, y + lineLength, width - radius - (2 * lineLength), height + (lineLength / 2) - (2 * lineLength));

            //textFont.Size
            textSize = gfx1.MeasureString(text, textFont);
            gfx1.DrawString(text, textFont, solidColorFont, x + radius + ((width - textSize.Width - radius) / 2), y + ((height - textSize.Height) / 2));
            //return gfx;
        }

        #region Public functions
        public void AddButton(int x, int y, int width, int height, string text, functionPointer function)
        {
            ButtonSettings_s newButton = new ButtonSettings_s();
            ButtonFontSettings_s font = new ButtonFontSettings_s();
            ColorSettings_s color = new ColorSettings_s();
            newButton.left = x;
            newButton.top = y;
            newButton.width = width;
            newButton.height = height;
            newButton.lineLength = lineLength;
            newButton.text = text;

            font.name = fontName;
            font.size = fontSize;
            font.style = fontStyle;
            newButton.font = font;

            color.lineColor = lineColor;
            color.foreColor = foreColor;
            color.fontColor = fontColor;
            color.blinkColor = blinkColor;
            newButton.color = color;

            newButton.function = function;

            buttons.Add(newButton);
            //return createGraphics(left, top, width, height, lineColor, foreColor);
            //createGraphics(gfx, x, y, width, height, lineColor, foreColor, blinkColor);
        }
        

        public void AddButton(int x, int y, int width, int height, Color line, Color fore, Color blink, string text)
        {
            ButtonSettings_s newButton = new ButtonSettings_s();
            ButtonFontSettings_s font = new ButtonFontSettings_s();
            ColorSettings_s color = new ColorSettings_s();

            newButton.left = x;
            newButton.top = y;
            newButton.width = width;
            newButton.height = height;
            newButton.lineLength = lineLength;
            newButton.text = text;

            font.name = fontName;
            font.size = fontSize;
            font.style = fontStyle;
            newButton.font = font;

            color.lineColor = lineColor;
            color.foreColor = foreColor;
            color.fontColor = fontColor;
            color.blinkColor = blinkColor;
            newButton.color = color;

            buttons.Add(newButton);

            //return createGraphics(x, y, width, height, line, fore);
            //createGraphics(gfx, x, y, width, height, line, fore, blink);
        }


        public void SetGFX(Graphics g)
        {
            gfx = g;
        }


        public void SetColors(Color line, Color fore, Color blink, Color font)
        {
            lineColor = line;
            foreColor = fore;
            blinkColor = blink;
            fontColor = font;
        }


        public void SetFontStyle(string name, int size, FontStyle style)
        {
            fontName = name;
            fontSize = size;
            fontStyle = style;
        }


        public void Draw()
        {
            int i;
            for (i = 0; i < buttons.Count; i++)
            {
                createGraphics(gfx, buttons[i].left, buttons[i].top, buttons[i].width, buttons[i].height,
                    buttons[i].color, buttons[i].text, buttons[i].font);
            }
        }


        public void PerformFunction(int X, int Y)
        {
            foreach (ButtonSettings_s newButton in buttons)
            {
                if ((X >= newButton.left) && (X <= newButton.left + newButton.width) &&
                    (Y >= newButton.top) && (Y <= newButton.top + newButton.height))
                {
                    newButton.function();
                }
            }
            //functionPointer ptr = new functionPointer(buttonSettings[0].function);
            //ptr();
        }
        #endregion
    }
}
